<html>
<head>
    <script src="../tma.js"></script>
    <script>
        tma.onload = function () {
            // All libraries are loaded asynchronously.
            tma.log("test.html: libraries must be loaded");
            var screen = new TmaScreen(640, 480);
            screen.attachTo(TmaScreen.BODY);
            screen.afterimage();
            screen.blur(0.25, 1.0, 1.0, 0, 2, false);

            function Snow () { TmaParticle.apply(this, arguments); }
            Snow.prototype = new TmaParticle(null, 0);
            Snow.prototype.constructor = Snow;
            Snow.GRAVITY = 10 / 1000;

            var container = new TmaParticle.Container(Snow);

            Snow.emit = function () {
                var x = Math.random() * screen.width;
                var vy = Math.random() + 0.5;
                container.add(x, vy);
            };

            Snow.prototype.initialize = function (x, vy) {
                this.y = 0;
                this.x = x;
                this.vy = vy;
            };

            Snow.prototype.update = function () {
                this.vy += Snow.GRAVITY;
                this.vy *= 0.99;
                this.y += this.vy;
                if (this.y >= screen.height)
                    return false;
                screen.setPixel(~~this.x, ~~this.y, 0xff, 0xff, 0xff, 0xff);
                return true;
            };

            function main () {
                for (i = 0; i < 20; i++)
                    Snow.emit();
                var data = screen.lock().data;
                for (var i = 0; i < screen.width * screen.height; i++) {
                    // RGBA
                    data[i * 4 + 0] = 0x00;
                    data[i * 4 + 1] = 0x00;
                    data[i * 4 + 2] = 0x00;
                    data[i * 4 + 3] = 0xff;
                }
                container.update();
                screen.unlock();
            }

            setInterval(main, 20);
        };
    </script>
</head>
</html>
